<script setup>
import {
    Search,
    Download,
    Promotion
} from '@element-plus/icons-vue'

import { ref ,onMounted} from 'vue'
import {getSportsPage,SprotsApply} from '@/api/user/sportsProject.js'
import {getSelectListService} from '@/api/sportsProject/sportsProject.js'
//导入消息框
import {ElMessage,ElMessageBox} from 'element-plus'
//导入token状态
import { useTokenStore } from '@/stores/token.js'
//分页条数据模型
const currentPage = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(6)//每页条数
//用户搜索时选中的分类id
const sportsName=ref()
//接受回显数据模型
const sportsPage=ref();
//创建学期
const select = ref([])
//学期
//弹窗标题
const title=ref('')
//控制添加分类弹窗
const dialogVisible = ref(false)
const year = ref(null)
const loading = ref(true)
const getSportsProjectPage = async () => {
    const getSportsProjectPageModel = {
    currentPage: currentPage.value,
    pageSize: pageSize.value,
    sportsName: sportsName.value ? sportsName.value: null,
    yearTermID:year.value ? year.value:null
    }
    let result = await getSportsPage(getSportsProjectPageModel);
    //渲染列表数据
    sportsPage.value = result.data.records;
    total.value=result.data.total;
    loading.value=false
}
const getSelectList =async () =>{
    let result = await getSelectListService();
    //渲染下拉框数据
    select.value = result.data;
    year.value=select.value[1].yearTermID
}
//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    getSportsProjectPage()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    currentPage.value = num
    getSportsProjectPage()
}
const addProject=async(row)=>{
    const model={
        yearTermID:year.value,
        sportsID:row.sportsID
    }
    ElMessageBox.confirm(
            '你确认报名项目：'+row.sportsName+'吗？',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                let result= await SprotsApply(model)
                ElMessage.success("报名成功！")
                getSportsProjectPage()
            })
            .catch(() => {
                //用户点击了取消
                ElMessage({
                    type: 'info',
                    message: '取消报名',
                })
            })
}
onMounted(
    ()=>{
        getSelectList().then(()=>{
            getSportsProjectPage()
        })
    }
)
const check=()=>{
    getSportsProjectPage()
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span><strong>第{{year}}学期运动会比赛项目信息</strong></span>
            </div>
        </template>
        <!-- 搜索表单 -->
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="学期：" style="width: 300px;">
                <el-select v-model="year" class="m-2" placeholder="全部"  @change="check">
                <el-option
                        v-for="item in select"
                        :key="item.yearTermID"
                        :label="item.yearTermName"
                        :value="item.yearTermID"
                />
            </el-select>
            </el-form-item>
            <el-form-item label="项目名(模糊查询)">
                <el-input placeholder="请输入项目名" v-model="sportsName" :suffix-icon="Search"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary"@click="input">搜索</el-button>
                <el-button @click="sportsName='',year=null,getSportsPage()">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 比赛项目列表 -->
        <el-table  v-loading="loading"
                element-loading-text="Loading..."
                :element-loading-spinner="svg"
                element-loading-svg-view-box="-10, -10, 50, 50"
                :data="sportsPage" style="width: 100%">
            <el-table-column label="项目编号" prop="sportsID" width="90%"></el-table-column>
            <el-table-column label="项目名称" prop="sportsName" show-overflow-tooltip effect="light"> </el-table-column>
            <el-table-column label="项目类型">
                <template #default="{ row }">
                    <el-text class="mx-1" type="primary"v-if="row.type ===1">单人项目</el-text>
                    <el-text class="mx-1" type="warning"v-else>团队项目</el-text>
                 </template>
            </el-table-column>
            <el-table-column label="报名限制数量" prop="limitNum1"width="110%"></el-table-column>
            <el-table-column label="已报(已选中)数量"show-overflow-tooltip>
                    <template #default="{ row }">
                       <strong> {{ row.totalNum }}({{ row.selectNum }})</strong>
                    </template>
            </el-table-column>
            <el-table-column label="项目备注" prop="sportsMemo"width="200"show-overflow-tooltip></el-table-column>
            <el-table-column label="相关附件" prop="fileName"show-overflow-tooltip>
                    <template #default="{ row }">
                        <el-tooltip content="点击下载" effect="light">
                    <el-link type="primary" :href="row.fileUrl" target="_blank" :download="row.fileName"
                    :underline="false">
                        {{ row.fileUrl?row.fileName:"" }}
                    </el-link>
                        </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-tooltip content="点击报名" effect="light">
                        <el-button v-if="row.selectNum<row.limitNum1 && row.flag===null" :icon="Promotion" circle plain type="primary" @click=addProject(row)></el-button>
                    </el-tooltip>
                    <el-text class="mx-1" type="warning"v-if="row.selectNum===row.limitNum1&&row.flag===null">人数已满</el-text>
                    <el-text class="mx-1" type="warning"v-if="row.flag===1||row.flag==2">已报名</el-text>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="未查找到数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[6, 8 ,10]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>